﻿using ETA.DomainEnum;
using ETA.Entity;
using ETA.IRepository;
using ETA.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ETA.Service
{
    public class RegistrationMainFormService : IRegistrationMainFormService
    {


        #region 属性

        private IRegistrationMainFormRepository _IRegistrationMainFormRepository { set; get; }
        private IFlowLogRepository _IFlowLogRepository { set; get; }
        private IFlowStepRepository _IFlowStepRepository { set; get; }

        #endregion

        #region 构造函数

        public RegistrationMainFormService(IRegistrationMainFormRepository IRegistrationMainFormRepository,
            IFlowLogRepository IFlowLogRepository, IFlowStepRepository IFlowStepRepository)
        {
            _IRegistrationMainFormRepository = IRegistrationMainFormRepository;
            _IFlowLogRepository = IFlowLogRepository;
            _IFlowStepRepository = IFlowStepRepository;
        }

        #endregion


        public CreateRegistrationMainFormResponse CreateRegistrationMainForm(CreateRegistrationMainFormRequest req)
        {
            var res = new CreateRegistrationMainFormResponse();
            try
            {
                if (req != null)
                {
                    res.RegistrationMainForm = _IRegistrationMainFormRepository.Insert(req.RegistrationMainForm);
                    res.Result.IsSuccess = true;
                }
            }
            catch (Exception e)
            {
                res.Result.ErrMessage = e.Message;
            }
            return res;
        }


        public ModifyRegistrationMainFormResponse ModifyRegistrationMainForm(ModifyRegistrationMainFormRequest req)
        {
            var res = new ModifyRegistrationMainFormResponse();
            try
            {
                if (req != null)
                {
                    //res.RegistrationMainForm = 
                    _IRegistrationMainFormRepository.Update(req.RegistrationMainForm);
                    res.Result.IsSuccess = true;
                }
            }
            catch (Exception e)
            {
                res.Result.ErrMessage = e.Message;
            }
            return res;
        }


        public QueryRegistrationMainFormResponse QueryRegistrationMainFormList(QueryRegistrationMainFormRequest req)
        {
            var response = new QueryRegistrationMainFormResponse();
            try
            {
                if (!string.IsNullOrEmpty(req.RegistrationMainFormId))
                {
                    response.RegistrationMainForms = _IRegistrationMainFormRepository.GetAllList(b => b.RegistrationMainFormId == req.RegistrationMainFormId);
                }
                else if (!string.IsNullOrEmpty(req.UserId))
                {
                    response.RegistrationMainForms = _IRegistrationMainFormRepository.GetAllList(b => b.User.UserId == req.UserId);
                }
                else
                {
                    response.RegistrationMainForms = _IRegistrationMainFormRepository.GetAllList();
                }

                //填充步骤 进行状态
                if (response.RegistrationMainForms.Any())
                {
                    var withFlowStepState = new List<RegistrationMainForm>();
                    foreach (var r in response.RegistrationMainForms)
                    {
                        withFlowStepState.Add(FillFlowStatus(r));
                    }
                    response.RegistrationMainForms = withFlowStepState;
                }
                response.Result.IsSuccess = true;
            }
            catch (Exception e)
            {
                response.Result.ErrMessage = e.Message;
            }
            return response;
        }

        /// <summary>
        ///  填充制定主流挂钩的 的 步骤进行状态
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        private RegistrationMainForm FillFlowStatus(RegistrationMainForm model)
        {
            var flowSteps = _IFlowStepRepository.GetAllList();
            var logs = model.FlowLogs;//已有流程步骤

            //处理是否完成状态
            foreach (var s in flowSteps)
            {
                s.StepStatusEnum = StepStatusEnum.未开启;//默认未开启
                if (logs.Any())
                {
                    if (logs.Any(l => l.FlowStepInfo.FlowStepId == s.FlowStepId))//有此条在步骤
                    {
                        var tempLog = logs.FirstOrDefault(l => l.FlowStepInfo.FlowStepId == s.FlowStepId);
                        var tempFlowLogState = tempLog.FlowLogStateEnum;
                        if (tempFlowLogState == FlowLogStateEnum.被拒绝 || tempFlowLogState == FlowLogStateEnum.进行中||tempFlowLogState== FlowLogStateEnum.可开启)//被拒绝和进行中可以一直修改
                        {
                            s.StepStatusEnum = StepStatusEnum.未完成;
                        }
                        else if (tempFlowLogState == FlowLogStateEnum.完成)
                        {
                            s.StepStatusEnum = StepStatusEnum.已完成;
                        }
                    }
                }
            }

            //检测是否可开启
            foreach (var s in flowSteps)
            {
                if ((s.FlowIndex == 1 || s.FlowIndex == 0) && s.StepStatusEnum == StepStatusEnum.未开启)//第一步和app特有步骤肯定可以开启可以开启 
                {
                    s.StepStatusEnum = StepStatusEnum.可开启;
                }
                //后面的步骤需检测其的上一步是否已完成
                if (s.FlowIndex > 1 && s.StepStatusEnum == StepStatusEnum.未开启)//并且这里只检测 未开启过的 步骤
                {
                    //表示当前步骤的上一步已完成时才可以进行开启
                    if (flowSteps.Find(f => f.FlowIndex == (s.FlowIndex - 1)).StepStatusEnum == StepStatusEnum.已完成)
                    {
                        s.StepStatusEnum = StepStatusEnum.可开启;
                    }
                    else
                    {
                        s.StepStatusEnum = StepStatusEnum.不可开启;
                    }
                }
            }
            model.FlowStepStatus = flowSteps;
            return model;
        }


        public bool UpdateCurrentStep(string userId, string flowStepId)
        {
            return _IRegistrationMainFormRepository.UpdateCurrentStep(userId, flowStepId);
        }
    }
}
